<template>
  <div class="purse_report">
    <div class="top_box">
      <dt-search ref="search" :xl="5" :xlBtn="4" @reset="reset">
        <Button type="primary" @click="onExport" v-if="unlocking('0215020301')">导出</Button>
      </dt-search>
    </div>
    <div class="mid_box">
      <div>钱包进账：{{ incomeAmount }}元</div>
      <div>钱包提现：{{ withdrawMoneyAmount }}元</div>
    </div>

    <dt-grid ref="table" :page="page" :onFetch="onFetch"></dt-grid>
  </div>
</template>

<script>
  import SearchOpt from './opt/search.js'
  import column from '../opt/dataColumn.js'
  import purseMgr from './biz/index.js'
  export default {
    data() {
      return {
        incomeAmount: '0',
        withdrawMoneyAmount: '0',
        page: {
          total: 0
        },
        parmas: {
          orgIds: [],
          housesId: null,
          housesName: null,
          userId: null,
          checkTime: []
        },
        tabsVal: '6',
        needData: {}
      }
    },

    mounted() {
      this.$refs.search.init(this.$refs.table, new SearchOpt(), this.parmas)
      this.$refs.table.init(this, new column(this))
      this.parmas.checkTime = [this.getCurrent('start'), this.getCurrent('end')]
      this.$refs.table.fetch()
    },

    methods: {
      onFetch(info) {
        this.needData = {
          ...info,
          startTime: this.parmas.checkTime[0] || null,
          endTime: this.parmas.checkTime[1] || null,
          userId: Number(this.parmas.userId) || null
        }
        return purseMgr.getWalletStatisticsWebList(this.needData).then((res) => {
          this.getPurseData(this.needData)
          this.page.total = res.total
          return res.list
        })
      },
      // 重置
      reset() {
        this.parmas.checkTime = [this.getCurrent('start'), this.getCurrent('end')]
      },
      getPurseData(needData) {
        purseMgr.getWalletStatistics(needData).then((res) => {
          if (res) {
            this.incomeAmount = res.incomeAmount
            this.withdrawMoneyAmount = res.withdrawMoneyAmount
          }
        })
      },
      onExport() {
        purseMgr.walletStatisticsWebListExport(this.needData)
      },
      getCurrent(type) {
        let date = new Date()
        let year = date.getFullYear()
        let month = date.getMonth() + 1
        if (type == 'start') {
          return `${year}-${month}-01 00:00:00`
        } else {
          return `${year}-${month}-${new Date(year, month, 0).getDate()} 23:59:59`
        }
      }
    }
  }
</script>

<style lang="scss" scoped>
  .purse_report {
    padding: 24px;
  }
  .mid_box {
    display: flex;
    div {
      margin-right: 36px;
      font-size: 16px;
    }
  }
</style>
